unit TargetFirebird;

interface

uses
  SysUtils, Classes, DB;

type
  TdmFirebird = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function GetFirebirdFieldType(FieldType: TFieldType; DataSize: integer)
  : string; overload;
function GetFirebirdFieldType(FieldType: string; DataSize: integer)
  : string; overload;

var
  dmFirebird: TdmFirebird;

implementation

{$R *.dfm}

function InArray(Item: string; List: array of string): Boolean;
var
  i: integer;
begin
  Result := False;
  for i := Low(List) to High(List) do
  begin
    if Item = List[i] then
    begin
      Result := True;
      Break;
    end;
  end;
end;

function GetFirebirdFieldType(FieldType: TFieldType; DataSize: integer): string;
const
  CharacterSet = ' CHARACTER SET WIN1251 ';
begin
  Result := '';
  case FieldType of
    ftUnknown:
      Result := '/* ftUnknown */ varchar(50)';
    ftString:
      Result := '/* ftString */ varchar';
    ftSmallint:
      Result := '/* ftSmallint */ integer';
    ftInteger:
      Result := '/* ftInteger */ integer';
    ftWord:
      Result := '/* ftWord */ integer';
    ftBoolean:
      Result := '/* ftBoolean */ integer';
    ftFloat:
      Result := '/* ftFloat */ bigint';
    ftCurrency:
      Result := '/* ftCurrency */ numeric';
    ftBCD:
      Result := '/* ftBCD */ bigint';
    ftDate:
      Result := '/* ftDate */ date';
    ftTime:
      Result := '/* ftTime */ time';
    ftDateTime:
      Result := '/* ftDateTime */ TimeStamp';
    ftBytes:
      Result := '/* ftBytes */ numeric';
    ftVarBytes:
      Result := '/* ftVarBytes */ numeric';
    ftAutoInc:
      Result := '/* ftAutoInc */ bigint';
    ftBlob:
      Result := '/* ftBlob */ blob';
    ftMemo:
      Result := '/* ftMemo */ varchar';
    ftGraphic:
      Result := '/* ftGraphic */ blob';
    ftFmtMemo:
      Result := '/* ftFmtMemo */ varchar';
    ftParadoxOle:
      ;
    ftDBaseOle:
      ;
    ftTypedBinary:
      Result := '/* ftTypedBinary */ blob sub_type text';
    ftCursor:
      ;
    ftFixedChar:
      ;
    ftWideString:
      Result := '/* ftWideString */ varchar';
    ftLargeint:
      Result := '/* ftLargeint */ bigint';
    ftADT:
      ;
    ftArray:
      ;
    ftReference:
      ;
    ftDataSet:
      ;
    ftOraBlob:
      Result := '/* ftOraBlob */ blob sub_type text';
    ftOraClob:
      Result := '/* ftOraClob */ blob sub_type text';
    ftVariant:
      Result := '/* ftVariant */ varchar';
    ftInterface:
      Result := '/* ftInterface */ numeric';
    ftIDispatch:
      Result := '/* ftIDispatch */ numeric';
    ftGuid:
      Result := '/* ftGuid */ numeric';
    ftTimeStamp:
      Result := '/* ftTimeStamp */ TimeStamp';
    ftFMTBcd:
      ;
    ftFixedWideChar:
      Result := '/* ftFixedWideChar */ varchar';
    ftWideMemo:
      Result := '/* ftWideMemo */ varchar';
    ftOraTimeStamp:
      Result := '/* ftOraTimeStamp */ TimeStamp';
    ftOraInterval:
      Result := '/* ftOraInterval */ TimeStamp';
  end;

  case FieldType of
    ftUnknown, ftString, ftMemo, ftFmtMemo, ftWideString, ftVariant,
      ftFixedWideChar, ftWideMemo:
      begin
        if DataSize <> 0 then
          Result := Result + '(' + IntToStr(DataSize) + ')';

        if (DataSize = 0) then // and (Result = 'varchar')
          Result := Result + '(100)';

        Result := Result + CharacterSet;
      end;
  end;
end;

function GetFirebirdFieldType(FieldType: string; DataSize: integer): string;
const
  CharacterSet = ' CHARACTER SET WIN1251 ';
begin
  // http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements001.htm
  // http://www.firebirdsql.org/manual/migration-mssql-data-types.html

  Result := ' /* ' + FieldType + ' */ ';

  if FieldType = 'CHAR' then
    Result := 'char'
  else if FieldType = 'VARCHAR' then
    Result := 'varhar'
  else if FieldType = 'NCHAR' then
    Result := 'char'
  else if FieldType = 'NVARCHAR' then
    Result := 'varchar'
  else if FieldType = 'NCHAR2' then
    Result := 'char'
  else if FieldType = 'NVARCHAR2' then
    Result := 'varchar'
  else if FieldType = 'NUMBER' then
    Result := 'decimal'
  else if FieldType = 'DATE' then
    Result := 'date'
  else if FieldType = 'BINARY_FLOAT' then
    Result := 'decimal ()'
  else if FieldType = 'BINARY_DOUBLE' then
    Result := 'decimal ()'
  else if FieldType = 'TIMESTAMP' then
    Result := 'integer'
  else if FieldType = 'TIMESTAMP [(fractional_seconds)] WITH TIME ZONE' then
    Result := 'integer'
  else if FieldType = 'TIMESTAMP [(fractional_seconds)] WITH LOCAL TIME ZONE'
  then
    Result := 'integer'
  else if FieldType = 'INTERVAL YEAR [(year_precision)] TO MONTH' then
    Result := 'decimal'
  else if FieldType = 'INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds)]'
  then
    Result := 'decimal'
  else if FieldType = 'RAW(size)' then
    Result := 'blob'
  else if FieldType = 'LONG RAW' then
    Result := 'blob'
  else if FieldType = 'ROWID' then
    Result := 'char()'
  else if FieldType = 'UROWID [(size)]' then
    Result := 'decimal'
  else if FieldType = 'CLOB' then
    Result := 'blob sub_type text'
  else if FieldType = 'NCLOB' then
    Result := 'blob sub_type text'
  else if FieldType = 'LONG' then
    Result := 'blob sub_type text'
  else if FieldType = 'BLOB' then
    Result := 'blob'
  else if FieldType = 'BFILE' then
    Result := 'nvarchar2()';


  if InArray(FieldType, ['CHAR', 'VARCHAR', 'NCHAR', 'NVARCHAR', 'NCHAR2',
    'NVARCHAR2', 'CLOB', 'NCLOB']) then
  begin
    if DataSize <> 0 then
      Result := Result + '(' + IntToStr(DataSize) + ')';

    if (DataSize = 0) then // and (Result = 'varchar')
      Result := Result + '(100)';

    Result := Result + CharacterSet;
  end;
end;

end.
